<script lang="ts" setup>
import type { Recordable } from '@vben/types';

import { onMounted } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { Input, message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';

const emit = defineEmits<{
  submit: [Recordable<any>];
}>();
// const data = ref();
const [Form, formApi] = useVbenForm({
  // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
    labelClass: 'w-2/6',
  },
  // 提交函数
  // handleSubmit: onSubmit,
  // 垂直布局，label和input在不同行，值为vertical
  // 水平布局，label和input在同一行
  layout: 'horizontal',
  schema: [
    // {
    //   // 组件需要在 #/adapter.ts内注册，并加上类型
    //   component: 'Input',
    //   fieldName: 'field',
    //   label: '自定义后缀',
    //   suffix: () => h('span', { class: 'text-red-600' }, '元'),
    // },
    // {
    //   component: 'Input',
    //   fieldName: 'field1',
    //   label: '自定义组件slot',
    //   renderComponentContent: () => ({
    //     prefix: () => 'prefix',
    //     suffix: () => 'suffix',
    //   }),
    // },
    // {
    //   component: 'Input',
    //   fieldName: 'field3',
    //   label: '自定义组件(slot)',
    //   rules: 'required',
    // },

    {
      component: 'Input',
      fieldName: 'name',
      label: '名称',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'transactionNum',
      label: '交易编号',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'orderNum',
      label: '订单编号',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'tradingTime',
      label: '交易时间',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'counterparties',
      label: '交易方',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'productName',
      label: '产品名称',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'type',
      label: '类型',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'payMethod',
      label: '支付方式',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'payWallId',
      label: '支付ID',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'price',
      label: '价格',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'status',
      label: '状态',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'remark',
      label: '备注',
      rules: 'required',
    },
  ],
  wrapperClass: 'grid-cols-2',
});

const [Modal, modalApi] = useVbenModal({
  onCancel() {
    modalApi.close();
  },
  // Modal确认
  async onConfirm() {
    const { valid } = await formApi.validate();
    const values = await formApi.getValues();
    if (valid) {
      message.success({
        content: `form values: ${JSON.stringify(values)}`,
      });
      emit('submit', values as { password: string; username: string });
    }
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      // data.value = modalApi.getData<Record<string, any>>();
    }
  },
});

onMounted(() => {
  formApi.setState({ submitButtonOptions: { show: false } });
  formApi.setState({ resetButtonOptions: { show: false } });
});
</script>
<template>
  <Modal title="添加账单明细">
    <!-- <div class="flex-col-center">外部传递数据： {{ data }}</div> -->
    <Form>
      <template #field3="slotProps">
        <Input placeholder="请输入" v-bind="slotProps" />
      </template>
    </Form>
  </Modal>
</template>
